/*******************************************************************
* If you include this file you have to:
* 	- register_dictiontary("dealers.txt")
*	- add: new const PREFIX[] = "[Your prefix]"; - this prefix will be displayed in chat commands
*
*
*********************************************************************/


#if defined _dealers_natives_included
	#endinput
#endif

#include "dealers_def.inl"

#define _dealers_natives_included


new const gStandardNatives[16][] = {
	"register_dealer",
	"register_dealer_by_id",
	"set_dealer_item_def",
	"get_dealers_count",
	"is_dealer_registered",
	
	"give_items_to_dealers",
	"give_items_to_dealer",
	"give_item_to_dealer",
	"clear_player_dealers",
	"set_player_dealers",
	
	"set_player_dealer",
	"clear_player_items",
	"set_player_items",
	"set_player_item",
	"dealer_menu",
	
	"item_menu"
}

stock register_def_natives(const prefix[]){
	new name[64];
	new len = charsmax(name);
	new count = sizeof(gStandardNatives);
	new nat[32];
	for(new i=0;i<count;i++){
		nat[0] = '^0';
		formatex(nat, 31, "_%s", gStandardNatives[i]);
		name[0] = '^0';
		formatex(name, len, "%s_%s", prefix, nat);
		register_native(name, nat);
	}
}


stock d_register_dealer_by_id(id , const name[MAX_DEALER_NAME_LEN], DealerRefreshAction:refreshItems = Dra_OnLost, refreshItemsCount = 0, bool:refreshItemsDropOld = false){
	if(!gDealerDef[id][Di_Registered]) {
		gDealerDef[id][Di_Registered] = 1;
		copy(gDealerDef[id][Di_Name], MAX_DEALER_NAME_LEN -1, name);		
		gDealerDef[id][Di_RefreshItems] = _:refreshItems;
		gDealerDef[id][Di_RefreshItemsMinCount] = refreshItemsCount;
		gDealerDef[id][Di_RefreshItemsDropOld] = refreshItemsDropOld;
		return id;
	}
	return NO_DEALER;
}

stock d_set_dealer_item_def(dealer, item, minCount, maxCount, minChance, maxChance, minCost, maxCost){
	gDealersItemsDefs[dealer][item][Ii_MinCount] = minCount;
	gDealersItemsDefs[dealer][item][Ii_MaxCount] = maxCount;
	gDealersItemsDefs[dealer][item][Ii_MinChance] = minChance;
	gDealersItemsDefs[dealer][item][Ii_MaxChance] = maxChance;
	gDealersItemsDefs[dealer][item][Ii_MinCost] = minCost;
	gDealersItemsDefs[dealer][item][Ii_MaxCost] = maxCost;
}

//{ Registering dealers and items
public _is_dealer_registered(iPlugin, iParams){	
	new id = get_param(1);
	if(id< MAX_DEALERS )
		return gDealerDef[id][Di_Registered];
	return 0;
}

public _register_dealer(iPlugin, iParams){
	new name[MAX_DEALER_NAME_LEN];
	get_string(1, name, MAX_DEALER_NAME_LEN -1);
	new DealerRefreshAction:action = DealerRefreshAction:get_param(2);
	new refreshItemsCount = get_param(3);
	new bool:refreshItemsDropOld = bool:get_param(4);
	for(new id =0;id<MAX_DEALERS;id++) {		
		if(d_register_dealer_by_id(id, name, action, refreshItemsCount,refreshItemsDropOld) != NO_DEALER)
			return id;
	}
	return NO_DEALER;
}

public _register_dealer_by_id(iPlugin, iParams){	
	new id = get_param(1);
	new name[MAX_DEALER_NAME_LEN];
	new DealerRefreshAction:action = DealerRefreshAction:get_param(3);
	new refreshItemsCount = get_param(4);
	new bool:refreshItemsDropOld = bool:get_param(5);
	d_register_dealer_by_id(id, name, action, refreshItemsCount,refreshItemsDropOld);
}

public _set_dealer_item_def(iPlugin, iParams){
	new dealer = get_param(1);
	new item = get_param(2);
	new iMinCount = max(0, get_param(3));
	new iMaxCount= max(iMinCount, get_param(4));
	
	new iMinChance = clamp(get_param(5), 0, 100);
	new iMaxChance =  clamp(get_param(6), iMinChance, 100);
		
	new iMinCost = max(0, get_param(7));
	new iMaxCost = max(iMinCost, get_param(8));
	
	d_set_dealer_item_def( dealer, item, iMinCount, iMaxCount, iMinChance, iMaxChance, iMinCost, iMaxCost);
}

public  _get_dealers_count(iPlugin, iParams){
	new count = 0;
	for(new id = 0;id<MAX_DEALERS; id++) {
		if(gDealerDef[id][Di_Registered])
			count++;
	}
	return count;
}

//}

//{ Giving items to dealers based on definition
stock d_give_items_to_dealers(bool:forceGive, bool:forceDropOld){
	for(new dealer=0;dealer<MAX_DEALERS;dealer++)
		d_give_items_to_dealer( dealer, forceGive, forceDropOld);
}

stock d_give_items_to_dealer(dealer, bool:forceGive=false, bool:forceDropOld=false){
	if(!gDealerDef[dealer][Di_Registered])
		return;
	new bool:give = forceGive;
	if(!give){
		switch(gDealerDef[dealer][Di_RefreshItems]){
			case Dra_OnLost: if(d_get_dealers_items_count(dealer) <= gDealerDef[dealer][Di_RefreshItemsMinCount]){
				give = true;
			}
			case Dra_Always: give = true;
		}
	}
	if(give) {
		new bool:clear = forceDropOld;
		if(!forceDropOld)
			clear= bool:gDealerDef[dealer][Di_RefreshItemsDropOld];
		for(new item=0;item<ITEMS;item++){
			if(clear)
				gDealersItems[dealer][item][Dii_Count] = 0;
			if(random_num(gDealersItemsDefs[dealer][item][Ii_MinChance],  gDealersItemsDefs[dealer][item][Ii_MaxChance]) < random_num(0, 100)){
				gDealersItems[dealer][item][Dii_Count] += random_num(gDealersItemsDefs[dealer][item][Ii_MinCount], gDealersItemsDefs[dealer][item][Ii_MaxCount]);
				gDealersItems[dealer][item][Dii_Cost] = random_num(gDealersItemsDefs[dealer][item][Ii_MinCost], gDealersItemsDefs[dealer][item][Ii_MaxCost]);
			}
		}
	}
}

public _give_items_to_dealers(iPlugin, iParams){
	new bool:forceGive = bool:get_param(1);
	new bool:forceDropOld = bool:get_param(2);
	d_give_items_to_dealers(forceGive, forceDropOld);
}
public _give_items_to_dealer(iPlugin, iParams){
	new dealer = get_param(1);
	new bool:forceGive = bool:get_param(2);
	new bool:forceDropOld = bool:get_param(3);
	d_give_items_to_dealer(dealer, forceGive, forceDropOld);
}

public _give_item_to_dealer(iPlugin, iParams){
	new dealer = get_param(1);
	new item = get_param(2);
	new bool:bAdd = bool:get_param(3);
	if(bAdd)
		gDealersItems[dealer][item][Dii_Count] += get_param(4);
	else 
		gDealersItems[dealer][item][Dii_Count] = get_param(4);
	gDealersItems[dealer][item][Dii_Cost] = get_param(5);
}

//}


//{ Setting available dealers and items 

stock d_set_player_items(id, bool:set=false){
	for(new item=0;item<ITEMS; item++)
		playerAvailableItems[id][item] = set;
}

stock d_set_player_dealers(id, bool:set=false){
	for(new dealer=0;dealer<MAX_DEALERS; dealer++)
		if(gDealerDef[dealer][Di_Registered])
			playerAvailableDealers[id][dealer] = set;
}

public _clear_player_dealers(iPlugin, iParams){
	new id = get_param(1);
	d_set_player_dealers(id, false);
}

public _set_player_dealers(iPlugin, iParams){
	new id = get_param(1);
	new dealers[MAX_DEALERS];
	new len = min(MAX_DEALERS , get_param(3));
	new clear = get_param(4);
	if(clear)
		d_set_player_dealers(id, false);
	get_array(2, dealers, len);
	for(new i=0;i<len;i++) {
		playerAvailableDealers[id][dealers[i]] = 1;
	}
}

public _set_player_dealer(iPlugin, iParams){
	new id = get_param(1);
	new dealer = get_param(2);
	new bool:set = bool:get_param(3);
	if(0<id<MAX_PLAYERS && NO_DEALER<dealer<MAX_DEALERS && gDealerDef[dealer][Di_Registered])
		playerAvailableDealers[id][dealer] = set;
}

public _clear_player_items(iPlugin, iParams){
	new id = get_param(1);
	if(0<id<MAX_PLAYERS) {
		d_set_player_items(id, false);
	}	
}

public _set_player_items(iPlugin, iParams){
	new id = get_param(1);
	new items[ITEMS];
	new len = min(ITEMS , get_param(3));
	new clear = get_param(4);
	if(clear)
		d_set_player_items(id, false);
	get_array(2, items, len);
	for(new i=0;i<len;i++) {
		playerAvailableItems[id][items[i]] = 1;
	}
}

public _set_player_item(iPlugin, iParams){
	new id = get_param(1);
	new item = get_param(2);
	new set = get_param(3);
	if(0<id<MAX_PLAYERS && -1<item<ITEMS)
		playerAvailableItems[id][item] = set;
}
//}

